{include="header"}

{if condition="$fsc->proveedor_s"}
<script type="text/javascript" src="view/js/nueva_compra.js"></script>
<script type="text/javascript">
   fs_nf0 = {#FS_NF0#};
   all_impuestos = {function="json_encode($fsc->impuesto->all())"};
   all_series = {function="json_encode($fsc->serie->all())"};
   proveedor = {function="json_encode($fsc->proveedor_s)"};
   nueva_compra_url = '{$fsc->url()}';
   fs_community_url = '{#FS_COMMUNITY_URL#}';
   
   {if condition="$fsc->empresa->recequivalencia"}
   tiene_recargo = true;
   {/if}
   
   $(document).ready(function() {
      usar_serie();
      
      $("#b_lineas").click(function(event) {
         event.preventDefault();
         $("#li_opciones").removeClass('active');
         $("#li_lineas").addClass('active');
         $("#div_opciones").hide();
         $("#div_lineas").show();
      });
      
      $("#b_opciones").click(function(event) {
         event.preventDefault();
         $("#li_lineas").removeClass('active');
         $("#li_opciones").addClass('active');
         $("#div_lineas").hide();
         $("#div_opciones").show();
      });
   });
</script>

<div class="modal" id="modal_articulos">
   <div class="modal-dialog" style="width: 99%; max-width: 1000px;">
      <div class="modal-content">
         <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">Buscar artículos</h4>
         </div>
         <div class="modal-body">
            <form id="f_buscar_articulos" name="f_buscar_articulos" action="{$fsc->url()}" method="post" class="form">
               <input type="hidden" name="codproveedor" value="{$fsc->proveedor_s->codproveedor}"/>
               <div class="container-fluid">
                  <div class="row">
                     <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="input-group">
                           <input class="form-control" type="text" name="query" autocomplete="off"/>
                           <span class="input-group-btn">
                              <button class="btn btn-primary" type="submit">
                                 <span class="glyphicon glyphicon-search"></span>
                              </button>
                           </span>
                        </div>
                        <label>
                           <input type="checkbox" name="con_stock" value="TRUE" onchange="buscar_articulos()"/>
                           sólo con stock
                        </label>
                     </div>
                     <div class="col-lg-6 col-md-6 col-sm-6">
                        <select class="form-control" name="codfamilia" onchange="buscar_articulos()">
                           <option value="">Todas las familias</option>
                           <option value="">------</option>
                           {loop="$fsc->familia->all()"}
                           <option value="{$value->codfamilia}">{$value->descripcion}</option>
                           {/loop}
                        </select>
                     </div>
                  </div>
               </div>
            </form>
         </div>
         <ul class="nav nav-tabs" id="nav_articulos" style="display: none;">
            <li id="li_mis_articulos"><a href="#" id="b_mis_articulos">Mi catálogo</a></li>
            <li id="li_kiwimaru"><a href="#" id="b_kiwimaru">Internet</a></li>
            <li id="li_nuevo_articulo"><a href="#" id="b_nuevo_articulo">Nuevo...</a></li>
         </ul>
         <div id="search_results"></div>
         <div id="kiwimaru_results"></div>
         <div id="nuevo_articulo" class="modal-body" style="display: none;">
            <form name="f_nuevo_articulo" action="{$fsc->url()}" method="post" class="form">
               <div class="form-group">
                  Referencia:
                  <input class="form-control" type="text" name="referencia" maxlength="18" autocomplete="off"/>
               </div>
               <div class="form-group">
                  Descripción:
                  <input class="form-control" type="text" name="descripcion" autocomplete="off"/>
               </div>
               <div class="form-group col-lg-6 col-md-6 col-sm-6">
                  <a href="{$fsc->familia->url()}">Familia</a>:
                  <select name="codfamilia" class="form-control">
                  {loop="$fsc->familia->all()"}
                     <option value="{$value->codfamilia}">{$value->descripcion}</option>
                  {/loop}
                  </select>
               </div>
               <div class="form-group col-lg-6 col-md-6 col-sm-6">
                  <a href="{$fsc->impuesto->url()}">IVA</a>:
                  <select name="codimpuesto" class="form-control">
                     {loop="$fsc->impuesto->all()"}
                     <option value="{$value->codimpuesto}"{if condition="$value->is_default()"} selected="selected"{/if}>{$value->descripcion}</option>
                     {/loop}
                  </select>
               </div>
               <div class="text-right">
                  <button class="btn btn-sm btn-primary" type="submit" onclick="new_articulo();return false;">
                     <span class="glyphicon glyphicon-floppy-disk"></span>
                     &nbsp; Guardar y seleccionar
                  </button>
               </div>
            </form>
         </div>
      </div>
   </div>
</div>

<form id="f_new_albaran" class="form" name="f_new_albaran" action="{$fsc->url()}" method="post">
   <input type="hidden" name="petition_id" value="{$fsc->random_string()}"/>
   <input type="hidden" id="numlineas" name="numlineas" value="0"/>
   <input type="hidden" name="proveedor" value="{$fsc->proveedor_s->codproveedor}"/>
   <div class="container-fluid">
      <div class="row">
         <div class="col-lg-8 col-md-8 col-sm-8">
            <h1>
               <a href="{$fsc->proveedor_s->url()}">{$fsc->proveedor_s->nombre}</a>
            </h1>
         </div>
         <div class="col-lg-2 col-md-2 col-sm-2">
            <div class="form-group">
               Fecha:
               <input type="text" name="fecha" class="form-control datepicker" value="{$fsc->today()}" autocomplete="off"/>
            </div>
         </div>
         <div class="col-lg-2 col-md-2 col-sm-2">
            <div class="form-group">
               Hora:
               <input type="text" name="hora" class="form-control" value="{$fsc->hour()}" autocomplete="off"/>
            </div>
         </div>
      </div>
   </div>
   
   <ul class="nav nav-tabs" role="tablist">
      <li class="active" id="li_lineas"><a href="#" id="b_lineas">Líneas</a></li>
      <li id="li_opciones"><a href="#" id="b_opciones">Más opciones...</a></li>
   </ul>
   
   <div class="table-responsive" id="div_lineas">
      <table class="table table-condensed">
         <thead>
            <tr>
               <th class="text-left">Referencia</th>
               <th class="text-left">Descripción</th>
               <th class="text-right" width="90">Cantidad</th>
               <th></th>
               <th class="text-right">PVP</th>
               <th class="text-right" width="90">Dto. %</th>
               <th class="text-right">Neto</th>
               <th class="text-right" width="115">IVA</th>
               <th class="text-right recargo">RE %</th>
               <th class="text-right irpf">IRPF %</th>
               <th class="text-right">Total</th>
            </tr>
         </thead>
         <tbody id="lineas_albaran"></tbody>
         <tbody>
            <tr class="bg-info">
               <td><input id="i_new_line" class="form-control" type="text" placeholder="Buscar para añadir..." autocomplete="off"/></td>
               <td colspan="5" class="text-right">Totales:</td>
               <td>
                  <div id="aneto" class="form-control text-right" style="font-weight: bold;">{$fsc->show_numero(0)}</div>
               </td>
               <td>
                  <div id="aiva" class="form-control text-right" style="font-weight: bold;">{$fsc->show_numero(0)}</div>
               </td>
               <td class="recargo">
                  <div id="are" class="form-control text-right" style="font-weight: bold;">{$fsc->show_numero(0)}</div>
               </td>
               <td class="irpf">
                  <div id="airpf" class="form-control text-right" style="font-weight: bold;">{$fsc->show_numero(0)}</div>
               </td>
               <td>
                  <input type="text" name="atotal" id="atotal" class="form-control text-right" style="font-weight: bold;"
                         value="0" onchange="recalcular()" autocomplete="off"/>
               </td>
            </tr>
         </tbody>
      </table>
   </div>
   
   <div class="container-fluid">
      <div class="row" id="div_opciones" style="display: none; padding-top: 20px;">
         <div class="col-lg-3 col-md-3 col-sm-3">
            <div class="form-group">
               <a href="{$fsc->agente->url()}">Empleado</a>:
               <select name="codagente" class="form-control">
                  <option value="{$fsc->agente->codagente}">{$fsc->agente->get_fullname()}</option>
                  {if condition="$fsc->user->admin"}
                     <option value="{$fsc->agente->codagente}">-----</option>
                     {loop="$fsc->agente->all()"}
                     <option value="{$value->codagente}">{$value->get_fullname()}</option>
                     {/loop}
                  {/if}
               </select>
            </div>
         </div>
         <div class="col-lg-3 col-md-3 col-sm-3">
            <div class="form-group">
               <a href="{$fsc->almacen->url()}">Almacén</a>:
               <select name="almacen" class="form-control">
                  {loop="$fsc->almacen->all()"}
                     {if condition="$value->is_default()"}
                     <option value="{$value->codalmacen}" selected='selected'>{$value->nombre}</option>
                     {else}
                     <option value="{$value->codalmacen}">{$value->nombre}</option>
                     {/if}
                  {/loop}
               </select>
            </div>
         </div>
         <div class="col-lg-3 col-md-3 col-sm-3">
            <div class="form-group">
               <a href="{$fsc->serie->url()}">Serie</a>:
               <select name="serie" class="form-control" id="codserie" onchange="usar_serie();recalcular();">
                  {loop="$fsc->serie->all()"}
                     {if condition="$value->codserie==$fsc->proveedor_s->codserie"}
                     <option value="{$value->codserie}" selected='selected'>{$value->descripcion}</option>
                     {else}
                     <option value="{$value->codserie}">{$value->descripcion}</option>
                     {/if}
                  {/loop}
               </select>
            </div>
         </div>
         <div class="col-lg-3 col-md-3 col-sm-3">
            <div class="form-group">
               <a href="{$fsc->divisa->url()}">Divisa</a>:
               <select name="divisa" class="form-control">
                  {loop="$fsc->divisa->all()"}
                     {if condition="$value->is_default()"}
                     <option value="{$value->coddivisa}" selected="selected">{$value->descripcion}</option>
                     {else}
                     <option value="{$value->coddivisa}">{$value->descripcion}</option>
                     {/if}
                  {/loop}
               </select>
            </div>
         </div>
         <div class="col-lg-3 col-md-3 col-sm-3">
            <div class="form-group">
               Núm. proveedor:
               <input class="form-control" type="text" name="numproveedor" autocomplete="off"/>
            </div>
         </div>
      </div>
      <div class="row">
         <div class="col-lg-6 col-md-6 col-sm-6">
            <button class="btn btn-sm btn-default" type="button" onclick="window.location.href='{$fsc->url()}';">
               <span class="glyphicon glyphicon-refresh"></span> &nbsp; Reiniciar
            </button>
         </div>
         <div class="col-lg-6 col-md-6 col-sm-6 text-right">
            <button class="btn btn-sm btn-primary" type="button" onclick="$('#modal_guardar').modal('show');">
               <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar...
            </button>
         </div>
      </div>
      <div class="row">
         <div class="col-lg-12" style="margin-top: 20px;">
            <div class="form-group">
               Observaciones:
               <textarea class="form-control" name="observaciones" rows="3"></textarea>
            </div>
         </div>
      </div>
   </div>
   
   <div class="modal fade" id="modal_guardar">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
               <h4 class="modal-title">Guardar como...</h4>
            </div>
            <div class="modal-body">
               {loop="$fsc->tipos_a_guardar()"}
               <div class="radio">
                  <label>
                     <input type="radio" name="tipo" value="{$value['tipo']}"{if condition="$value['tipo']==$fsc->tipo"} checked="checked"{/if}/>
                     {$value['nombre']}
                  </label>
               </div>
               {/loop}
               <div class="form-group">
                  <a href="{$fsc->forma_pago->url()}">Forma de pago</a>:
                  <select name="forma_pago" class="form-control">
                  {loop="$fsc->forma_pago->all()"}
                     {if condition="$value->is_default()"}
                     <option value="{$value->codpago}" selected="selected">{$value->descripcion}</option>
                     {else}
                     <option value="{$value->codpago}">{$value->descripcion}</option>
                     {/if}
                  {/loop}
                  </select>
               </div>
            </div>
            <div class="modal-footer">
               <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                  <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
               </button>
            </div>
         </div>
      </div>
   </div>
</form>
{elseif condition="!$fsc->user->get_agente()"}
<div class="well well-lg">
   <h1>No puedes entrar aquí</h1>
   <p>
      No tienes un empleado asociado a tu <a href="{$fsc->user->url()}">usuario</a>.
      Habla con el administrador para que te asigne un empleado.
   </p>
   <p>Si crees que es un error de FacturaScripts, haz clic en el botón de ayuda, arriba a la derecha, e infórmanos del error.</p>
</div>
{else}
<script type="text/javascript">
   $(document).ready(function() {
      $("#modal_proveedor").modal('show');
      document.f_nueva_compra.ac_proveedor.focus();
      $("#ac_proveedor").autocomplete({
         serviceUrl: '{$fsc->url()}',
         paramName: 'buscar_proveedor',
         onSelect: function (suggestion) {
            if(suggestion)
            {
               if(document.f_nueva_compra.proveedor.value != suggestion.data)
               {
                  document.f_nueva_compra.proveedor.value = suggestion.data;
               }
            }
         }
      });
   });
</script>

<form name="f_nueva_compra" class="form" action="{$fsc->url()}" method="post">
   <input type="hidden" name="proveedor"/>
   <div class="modal" id="modal_proveedor">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
               <h4 class="modal-title">Selecciona el proveedor</h4>
            </div>
            <div class="modal-body">
               <div class="form-group">
                  <input class="form-control" type="text" name="ac_proveedor" id="ac_proveedor" placeholder="Buscar" autocomplete="off"/>
                  <p class="help-block"><a href="{$fsc->proveedor->url()}#nuevo" target="_blank">Nuevo proveedor</a>.</p>
               </div>
            </div>
            <div class="modal-footer">
               <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                  <span class="glyphicon glyphicon-play"></span> &nbsp; Seleccionar
               </button>
            </div>
         </div>
      </div>
   </div>
</form>

<div class="container" style="margin-top: 10px; margin-bottom: 100px;">
   <div class="row">
      <div class="col-lg-12">
         <h1>Paso 1:</h1>
         <p>Selecciona el proveedor al que quieres realizar la compra.</p>
         <a href="#" class="btn btn-block btn-default" data-toggle="modal" data-target="#modal_proveedor">Selecciona el proveedor</a>
         <h2>Paso 2:</h2>
         <p>Introduce línea a línea todos los artículos de la compra.</p>
         <h2>Paso 3:</h2>
         <p>Pulsa el botón guardar.</p>
      </div>
   </div>
</div>
{/if}

{include="footer"}